﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FBConnectAuth;
using NutriLeaf.Web.Model;

namespace NutriLeaf.Web.View.All
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


            

            FBConnectAuthentication auth = new FBConnectAuthentication(Constants.AppSettings.Facebook.ID, Constants.AppSettings.Facebook.Secret); //Note this is the "app id", not "api Key"
            
            if (auth.Validate() != ValidationState.Valid)
            {
                Response.Write("Invalid");
            }
            else
            {
                FBConnectSession fbSession = auth.GetSession();
                int userId = Convert.ToInt16(fbSession.UserID);
                string sessionKey = fbSession.SessionKey;
                string accessToken = fbSession.SessionSecret; //This is the Graph API access token (available only when using the Graph API)
                
                //if user already exists
                NutriLeafDBEntities context=new NutriLeafDBEntities();
                if (Core.Account.User.IsRegistered(userId, ref context))
                {
                    Model.User user=Core.Account.User.GetUser(userId, ref context);

                    if (user.Role.Name == Constants.Account.Roles.Admin)
                    {
                        //redirect to admin page
                        Response.Redirect(Constants.View.Admin.Default.DefaultPage);
                    }

                    if (user.Role.Name == Constants.Account.Roles.User)
                    {
                        //work in progress
                    }



                }
                //check the roles and redirect


                //else create a new user and redirect



            }
        }
    }
}